//
//  TTDiamondViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/8/23.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDiamondViewCell.h"
#import <SDWebImageIndicator.h>
#import <SDWebImage.h>

@interface TTDiamondViewCell ()

@property (weak, nonatomic) IBOutlet UIButton *luoshiBtn;
@property (weak, nonatomic) IBOutlet UIView *middleView;

@property (weak, nonatomic) IBOutlet UIImageView *leftImageView;
@property (weak, nonatomic) IBOutlet UIButton *locationBtn;     // 地点
@property (weak, nonatomic) IBOutlet UILabel *toTime;   // 到达时间
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;   // 标题
@property (weak, nonatomic) IBOutlet UILabel *nkLabel;      // 奶咖
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;
@property (weak, nonatomic) IBOutlet UIImageView *videoView;
@property (weak, nonatomic) IBOutlet UILabel *updateTime;//更新时间
@property (weak, nonatomic) IBOutlet UILabel *size;//尺寸描述
@property (weak, nonatomic) IBOutlet UIImageView *bgImg;


@property (weak, nonatomic) IBOutlet UILabel *shapeLabel;   // 形状
@property (weak, nonatomic) IBOutlet UILabel *discountLabel;    // 折扣
@property (weak, nonatomic) IBOutlet UILabel *discountRateLabel;

@property (weak, nonatomic) IBOutlet UIButton *zsBtn;   // 证书
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;   // 价格
@property (weak, nonatomic) IBOutlet UILabel *ticketLabel;  // 优惠券
@property (weak, nonatomic) IBOutlet UIButton *shopCartBtn;    // 购物车
@property (weak, nonatomic) IBOutlet UIView *lineView;      // 线条

@property (nonatomic, strong) TTDiamondModel *model;
@property (nonatomic, assign) CllType cellType;

@property (nonatomic, assign) BOOL isExchangeLuoshi;

///是否看过的标签
@property (weak, nonatomic) IBOutlet UILabel *seenLabel;

@end

@implementation TTDiamondViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    self.discountLabel.layer.cornerRadius = 3;
    self.discountLabel.layer.borderWidth = 0.5;
    
    self.shapeLabel.preferredMaxLayoutWidth = 200 * kScale;
    
    self.leftImageView.layer.cornerRadius = 5;
    self.leftImageView.layer.masksToBounds = YES;
    
    self.bgImg.layer.cornerRadius = 5;
    self.bgImg.layer.masksToBounds = YES;
    self.bgImg.backgroundColor = [UIColor whiteColor];
    self.middleView.backgroundColor = [UIColor clearColor];
    
    self.seenLabel.layer.cornerRadius = 3;
    self.seenLabel.layer.backgroundColor = [UIColor redColor].CGColor;
    
}

- (void)setModel:(TTDiamondModel *)model clarityCharacteristicsDic:(NSDictionary *)dic toTimeDic:(NSDictionary *)toTimeDic findShapeImageDic:(NSDictionary *)ShapeImageDic cellType:(CllType)cellType showDiscount:(BOOL)showDiscount isExchangeLuoshi:(BOOL)isExchangeLuoshi
{
    [self setModel:model clarityCharacteristicsDic:dic toTimeDic:toTimeDic findShapeImageDic:ShapeImageDic cellType:cellType showDiscount:showDiscount];
    
    self.isExchangeLuoshi = isExchangeLuoshi;
    
    if (isExchangeLuoshi) {
        //查询可更换裸石的
        [self.shopCartBtn setImage:[UIImage imageNamed:@"shopcart_unsel"] forState:UIControlStateNormal];
        [self.shopCartBtn setImage:[UIImage imageNamed:@"shopcartlist_sel"] forState:UIControlStateSelected];
        
        self.shopCartBtn.selected = model.selectExchange;
    }
    
    
}


- (void)setModel:(TTDiamondModel *)model clarityCharacteristicsDic:(NSDictionary *)dic toTimeDic:(nonnull NSDictionary *)toTimeDic findShapeImageDic:(nonnull NSDictionary *)ShapeImageDic cellType:(CllType)cellType showDiscount:(BOOL)showDiscount
{
    self.model = model;
    self.cellType = cellType;
    
    [self setupSubViews];
    
    NSString *diaSize = [NSString stringWithFormat:@"%.2f", model.DiaSize.floatValue];
    NSString *title = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@", model.Shape, diaSize, model.Color, model.Clarity, model.Cut, model.Sym, model.Polish, model.Flour];
    NSString *nkStr = [NSString stringWithFormat:@"%@  %@  %@", model.Milky, model.Browness, model.EyeClean];
    NSString *updateTime = [NSString stringWithFormat:@"%@",model.UpdateTime];
    NSString *showSizeStr = [NSString stringWithFormat:@"尺寸%@ 全深%@ 台宽%@",model.Measurement,model.FullDepth,model.DiaTable];
    
    self.size.text = showSizeStr;
    self.updateTime.text = updateTime;
    self.titleLabel.text = title;
    self.nkLabel.text = nkStr;
    NSString *value = @"";
    if (model.ClarityCharacteristics.length > 1) {
        NSArray *characteristicsArray = [model.ClarityCharacteristics componentsSeparatedByString:@","];
        for (NSString *str in characteristicsArray) {
            NSString *key = [str removeFirstAndLastBlank];
            value = [NSString stringWithFormat:@"%@%@ ", value, dic[key]];
        }
    }
    self.shapeLabel.text = value;
    
    NSString *statusStr = @"";
    switch (model.SysStatus) {
        case 0:
            statusStr = @"订货";
            break;
        case 1:
            statusStr = @"在库";
            break;
        case 2:
            statusStr = @"预售";
            break;
        case 3:
            statusStr = @"借出";
            break;
        case 4:
            statusStr = @"锁定";
            break;
        case 9:
            statusStr = @"已售";
            break;

        default:
            break;
    }
    
    NSString *locationStr = [NSString stringWithFormat:@"%@%@",model.Location,statusStr];
    [self.locationBtn setTitle:locationStr forState:UIControlStateNormal];
    
    // 折扣显示隐藏
    self.discountLabel.hidden = !showDiscount;
    self.discountRateLabel.hidden = self.discountLabel.hidden;
    self.pictureView.hidden = self.discountLabel.hidden;
    self.videoView.hidden = self.discountLabel.hidden;
    // 图片显示隐藏
    if (showDiscount) {
        self.pictureView.hidden = !model.HavePicture;
        self.videoView.hidden = model.Video.length <= 1;
    }
    
    if (self.pictureView.hidden) {
        [self.videoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pictureView);
        }];
    } else {
        [self.videoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.pictureView.mas_right).mas_offset(5);
        }];
    }
    // 折扣
    self.discountRateLabel.text = model.Disc;
    if (model.Discount >= 0) {
        self.discountLabel.textColor = UIColorHex(FF1F1F);
        self.discountLabel.layer.borderColor = UIColorHex(FF1F1F).CGColor;
        self.discountRateLabel.textColor = UIColorHex(FF1F1F);
    } else {
        self.discountLabel.textColor = UIColorHex(2ECC71);
        self.discountLabel.layer.borderColor = UIColorHex(2ECC71).CGColor;
        self.discountRateLabel.textColor = UIColorHex(2ECC71);
    }
    
    if (model.SouthAfrica) {
        // 来自南非
        NSMutableAttributedString *zsAttr = [[NSMutableAttributedString alloc] initWithString:@"South Africa"];
        [self.zsBtn setAttributedTitle:zsAttr forState:UIControlStateNormal];
        self.zsBtn.enabled = NO;
    } else {
        if (model.Certificate.length > 0) {
            NSString *zsStr = [NSString stringWithFormat:@"%@ %@ 证书预览", model.Cert, model.CertNo];
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:zsStr];
            [attr addAttributes:@{NSUnderlineStyleAttributeName : @1, NSUnderlineColorAttributeName : kMainColor, NSForegroundColorAttributeName : kMainColor} range:[zsStr rangeOfString:@"证书预览"]];
            
            [self.zsBtn setAttributedTitle:attr forState:UIControlStateNormal];
            self.zsBtn.enabled = YES;
        } else {
            NSString *zsStr = [NSString stringWithFormat:@"%@ %@", model.Cert, model.CertNo];
            NSMutableAttributedString *zsAttr = [[NSMutableAttributedString alloc] initWithString:zsStr];
            [self.zsBtn setAttributedTitle:zsAttr forState:UIControlStateNormal];
            self.zsBtn.enabled = NO;
        }
    }
    
    
    NSString *priceStr = [NSString stringWithFormat:@"人民币￥%@", model.Price];
    NSMutableAttributedString *priceAttrStr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [priceAttrStr addAttributes:@{NSForegroundColorAttributeName : UIColorHex(666666), NSFontAttributeName : [UIFont systemFontOfSize:13]} range:[priceStr rangeOfString:@"人民币"]];
    [priceAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11] range:[priceStr rangeOfString:@"￥"]];
    self.priceLabel.attributedText = priceAttrStr;
    
    self.luoshiBtn.selected = model.isSelect;
    
    // 到达深圳时间
    self.toTime.text = toTimeDic[model.Location];
    
    NSString *shapeImageName = ShapeImageDic[model.Shape];
    UIImage *image = [UIImage imageNamed:shapeImageName ? : @"Detail_12"];
//    self.leftImageView.image = image;
    
    self.shopCartBtn.selected = model.isInCart;
    self.ticketLabel.text = model.couponMoney;
    
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_lessThanOrEqualTo(250 * kScale);
    }];
    
//    [self.priceLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.contentView).mas_offset(-4);
//    }];
    
    //显示裸石真正图片
    if (showDiscount && model.DayLight.length > 0) {
        self.leftImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.leftImageView.sd_imageIndicator = [SDWebImageActivityIndicator grayIndicator];
        @weakify(self)
        [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.DayLight] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            @strongify(self)
            if (image == nil || error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.leftImageView.image = image;
                });
            }
        }];
    } else {
        self.leftImageView.image = image;
        self.leftImageView.contentMode = UIViewContentModeCenter;
    }
    
    self.seenLabel.hidden = !model.isSeen;
    
}

- (IBAction)clickSelectBtn:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    self.model.isSelect = sender.selected;
    if ([self.delegate respondsToSelector:@selector(clickSelectLuoshiButtonWithDiamondViewCell:button:model:)]) {
        [self.delegate clickSelectLuoshiButtonWithDiamondViewCell:self button:sender model:self.model];
    }
}

// 添加到购物车
- (IBAction)shopCartBtnClick:(UIButton *)sender {
    
    if (self.isExchangeLuoshi) {
        
        if ([self.delegate respondsToSelector:@selector(clickSelectCellWithDiamondViewCell:button:model:)]) {
            [self.delegate clickSelectCellWithDiamondViewCell:self button:sender model:self.model];
        }
        
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(clickShopCartWithDiamondViewCell:button:model:)]) {
        [self.delegate clickShopCartWithDiamondViewCell:self button:sender model:self.model];
    }
}


// 查看证书
- (IBAction)zsBtnClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(clickVerifyBtnWithDiamondViewCell:certificateUrlString:)]) {
        [self.delegate clickVerifyBtnWithDiamondViewCell:self certificateUrlString:self.model.Certificate];
    }
    
}


- (void)setupSubViews
{
    if (self.cellType == CllTypeWithCanChoose) {
        // 可选中的
        [self.middleView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.luoshiBtn.mas_right);
            make.right.equalTo(self.contentView).mas_offset(-10);
        }];
        
        self.luoshiBtn.hidden = NO;
        self.shopCartBtn.hidden = YES;
        self.lineView.hidden = NO;
        
    } else {
        [self.middleView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).mas_offset(24);
            make.right.equalTo(self.shopCartBtn.mas_left);
        }];
        
        self.luoshiBtn.hidden = YES;
        self.shopCartBtn.hidden = NO;
        self.lineView.hidden = YES;
    }
}

@end

